const express = require('express')
const path = require('path')
const cookieParser = require('cookie-parser')
const logger = require('morgan')
const cors = require('cors')
require('dotenv').config()

// 认证中间件
const adminAuth = require('./middlewares/admin-auth')
const userAuth = require('./middlewares/user-auth')


// 前台文件
const indexRouter = require('./routes/index')
const usersRouter = require('./routes/users')
const authRouter = require('./routes/auth')
const categoriesRouter = require('./routes/categories')
const productsRouter = require('./routes/products')
const cartsRouter = require('./routes/carts')
const addressesRouter = require('./routes/addresses')
const ordersRouter = require('./routes/orders')
const alipayRouter = require('./routes/alipay')


// 后台文件
const adminAuthRouter = require('./routes/admin/auth')
const adminHomeRouter = require('./routes/admin/home')
const adminCategoriesRouter = require('./routes/admin/categories')
const adminUploadsRouter = require('./routes/admin/uploads')
const adminTagsRouter = require('./routes/admin/tags')
const adminProductsRouter = require('./routes/admin/products')
const adminAdvertsRouter = require('./routes/admin/adverts')
const adminNoticesRouter = require('./routes/admin/notices')
const adminExpressesRouter = require('./routes/admin/expresses')
const adminUsersRouter = require('./routes/admin/users')
const adminOrdersRouter = require('./routes/admin/orders')
const adminBrandsRouter = require('./routes/admin/brands')
const adminArticlesRouter = require('./routes/admin/articles')
const adminChartsRouter = require('./routes/admin/charts')
const adminRolesRouter = require('./routes/admin/roles')
const adminPermissionsRouter = require('./routes/admin/permissions')


const app = express()
app.use(cors())
app.use(logger('dev'))
app.use(express.json());
app.use(express.urlencoded({extended: false}))
app.use(cookieParser())
app.use(express.static(path.join(__dirname, 'public')))

// 前台接口路由
app.use('/', indexRouter)
app.use('/users', usersRouter)
app.use('/auth', authRouter)
app.use('/categories', categoriesRouter)
app.use('/products', productsRouter)
app.use('/carts', userAuth(), cartsRouter)
app.use('/addresses', userAuth(), addressesRouter)
app.use('/orders', userAuth(), ordersRouter)
app.use('/alipay', alipayRouter)


// 后台接口路由
app.use('/admin/home', adminAuth(), adminHomeRouter)
app.use('/admin/categories', adminAuth(), adminCategoriesRouter)
app.use('/admin/uploads', adminUploadsRouter)
app.use('/admin/tags', adminAuth(), adminTagsRouter)
app.use('/admin/products', adminAuth(), adminProductsRouter)
app.use('/admin/adverts', adminAuth(), adminAdvertsRouter)
app.use('/admin/notices', adminAuth(), adminNoticesRouter)
app.use('/admin/expresses', adminAuth(), adminExpressesRouter)
app.use('/admin/users', adminAuth(), adminUsersRouter)
app.use('/admin/orders', adminAuth(), adminOrdersRouter)
app.use('/admin/brands', adminAuth(), adminBrandsRouter)
app.use('/admin/articles', adminAuth(), adminArticlesRouter)
app.use('/admin/charts', adminAuth(), adminChartsRouter)
app.use('/admin/roles', adminAuth(), adminRolesRouter)
app.use('/admin/permissions', adminAuth(), adminPermissionsRouter)
app.use('/admin/auth', adminAuthRouter)


module.exports = app;
